package Multithreading.Exercise012;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

/**
 * ClassName:MyCallable
 * Package:Multithreading.Exercise012
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/7-16:28
 * @Version:v1.0
 */
public class MyCallable implements Callable<Integer> {
    //数组,集合{10,5,20,50,100,200,500,800,2,80,300,700}
    ArrayList<Integer> list;

    public MyCallable(ArrayList<Integer> list) {
        this.list = list;
    }
    @Override
    public Integer call() throws Exception {
        ArrayList<Integer> boxList = new ArrayList<>();
        while (true){
            synchronized (MyCallable.class){
                if (list.size()==0){
                    System.out.println(Thread.currentThread().getName()+boxList);
                    break;
                }else {
                    Collections.shuffle(list);
                    int prize = list.remove(0);
                    boxList.add(prize);
                }
            }
            Thread.sleep(10);

        }
        //把集合最大值返回
        if(boxList.size()==0){
            return null;
        }else {
            return Collections.max(boxList);
        }

    }
}
